<?php

namespace App\Jobs;

use App\Libraries\CustomsBills\Data\CEB311Message;
use App\Libraries\CustomsBills\Message;
use App\Libraries\Warehouse\Docking;
use App\Libraries\Warehouse\Strategies\GetProduct;
use App\Models\Order;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Throwable;

class DockingJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    /**
     * @var Order
     * @author luffyzhao@vip.126.com
     */
    private $order;

    /**
     * Create a new job instance.
     *
     * @param Order $order
     */
    public function __construct(Order $order)
    {
        $this->order = $order;
    }

    /**
     * Execute the job.
     *
     * @return void
     * @throws Throwable
     */
    public function handle()
    {
        if($this->order->status !== 0)
        {
            Log::warning("订单{$this->order->order_code}不是申报中的状态");
            return ;
        }
        $this->order->loadMissing(['orderProducts']);
        try{


            $this->order->fill(['status' => 2, 'remark'=> '待发货'])->saveOrFail();
        }catch (Throwable | Exception $exception){
            Log::error("订单{$this->order->order_code}对接失败:" . $exception->getMessage());
            $this->order->fill(['status' => 1, 'remark'=> '对接失败'])->saveOrFail();
        }
    }


}
